package com.lanyou.esb.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonDeserializer;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.cook.entity.domain.DataDictionaryValue;
import com.lanyou.cook.entity.domain.InterfaceScheduleEntityListener;
import com.lanyou.esb.datadictionary.EnableType;

@Entity
@EntityListeners({ AuditingEntityListener.class})
@Table(name = "T_ESB_BU_IF_SCHEDULE")
public class IfSchedule implements Serializable{

		// 调度ID
		private String id;
		
		// 调度名称
		private String name;
		
		//接口类型
		private String ifType;

		// 执行周期单位
		private String runWay;
		
		// 执行周期
		private int runCyc;

		// 周执行日
		private String dayOfWeek;

		/*// 执行状态
		private String runStatus;*/

		// 开始日期
		private Date beginTime;

		// 结束日期
		private Date endTime;

		/*private String fileCachePath;*/

		private String creator;

		private String modifier;

		private Date createTime;

		private Date modifyTime;
		
		private Ftp ftp;

		
	    public final static Specification<IfSchedule> EmptySpecification = new Specification<IfSchedule>(){
			@Override
			public Predicate toPredicate(Root<IfSchedule> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				return null;
			}
		};
		
		public static Specification<IfSchedule> idSpecification(String id){
			return new Specification<IfSchedule>(){
				@Override
				public Predicate toPredicate(Root<IfSchedule> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					query.where(cb.equal(root.get("id"), id));
					return null;
				}
			};
		}
		
		@Id
		@GeneratedValue(generator = "uuidGenerator")
		@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
		@Column(name = "IF_SCHEDULE_ID", length = 36)
		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}
		
		@Column(name = "IF_TYPE", length = 2)
		public String getIfType() {
			return ifType;
		}

		public void setIfType(String ifType) {
			this.ifType = ifType;
		}

		@Column(name = "IF_SCHEDULE_NAME", nullable = false, length = 50)
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}


		@Column(name = "RUN_WAY", length = DataDictionaryValue.MAX_LENGTH_OF_CODE)
		public String getRunWay() {
			return runWay;
		}

		public void setRunWay(String runWay) {
			this.runWay = runWay;
		}

		@Column(name = "RUN_CYC", length = 2)
		public int getRunCyc() {
			return runCyc;
		}

		public void setRunCyc(int runCyc) {
			this.runCyc = runCyc;
		}

		@Column(name = "DAY_Of_WEEK", length = 100)
		public String getDayOfWeek() {
			return dayOfWeek;
		}

		public void setDayOfWeek(String dayOfWeek) {
			this.dayOfWeek = dayOfWeek;
		}

		/*@Column(name = "RUN_STATUS", length = 2)
		public String getRunStatus() {
			return runStatus;
		}

		public void setRunStatus(String runStatus) {
			this.runStatus = runStatus;
		}*/

		@Column(name = "SKD_BEGIN_DATE")
		@JsonSerialize(using = DefaultDateJsonSerializer.class)
		@JsonDeserialize(using = DefaultDateJsonDeserializer.class)
		public Date getBeginTime() {
			return beginTime;
		}

		public void setBeginTime(Date beginTime) {
			this.beginTime = beginTime;
		}

		@Column(name = "SKD_END_DATE")
		@JsonSerialize(using = DefaultDateJsonSerializer.class)
		@JsonDeserialize(using = DefaultDateJsonDeserializer.class)
		public Date getEndTime() {
			return endTime;
		}

		public void setEndTime(Date endTime) {
			this.endTime = endTime;
		}

		/*@Column(name = "FILE_CACHE_PATH", length = 100)
		public String getFileCachePath() {
			return fileCachePath;
		}

		public void setFileCachePath(String fileCachePath) {
			this.fileCachePath = fileCachePath;
		}*/

		@CreatedBy
		@Column(length = 50, updatable=false)
		public String getCreator() {
			return creator;
		}

		public void setCreator(String creator) {
			this.creator = creator;
		}

		@LastModifiedBy
		@Column(length = 50)
		public String getModifier() {
			return modifier;
		}

		public void setModifier(String modifier) {
			this.modifier = modifier;
		}

		@Column(name = "CREATED_DATE", updatable=false)
		@JsonSerialize(using = DefaultDateJsonSerializer.class)
		@CreatedDate
		public Date getCreateTime() {
			return createTime;
		}

		public void setCreateTime(Date createTime) {
			this.createTime = createTime;
		}

		@Column(name = "LAST_UPDATED_DATE")
		@JsonSerialize(using = DefaultDateJsonSerializer.class)
		@LastModifiedDate
		public Date getModifyTime() {
			return modifyTime;
		}

		public void setModifyTime(Date modifyTime) {
			this.modifyTime = modifyTime;
		}

		@ManyToOne
		@JoinColumn(name = "IF_ID")
		public Ftp getFtp() {
			return ftp;
		}

		public void setFtp(Ftp ftp) {
			this.ftp = ftp;
		}
		
		
		@Transient
		public String getIsEnable(){
			return EnableType.ENABLE.ValueCode;
		}
		
		@Transient
		public String getBeginTimeForExport(){
			if(beginTime != null){
				return beginTime.toString().substring(0, beginTime.toString().length() - 2);
			}else{
				return "";
			}
			
		}
		
		@Transient
		public String getEndTimeForExport(){
			if(endTime != null){
				return endTime.toString().substring(0, endTime.toString().length() - 2);
			}else{
				return "";
			}
		}
		
		
		@Transient
		public String getCreateTimeForExport(){
			if(createTime != null){
				return createTime.toString().substring(0, createTime.toString().length() - 2);
			}else{
				return "";
			}
			
		}
		
		@Transient
		public String getModifyTimeForExport(){
			if(modifyTime != null){
				return modifyTime.toString().substring(0, modifyTime.toString().length() - 2);
			}else{
				return "";
			}
		}
	}
